//
//  WCRegisterViewController.m
//  WeChat
//
//  Created by lin on 15/7/17.
//  Copyright (c) 2015年 lin. All rights reserved.
//

#import "WCRegisterViewController.h"
#import "AppDelegate.h"
@interface WCRegisterViewController()
@property (weak, nonatomic) IBOutlet UITextField *userField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *leftConstraint;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *rightConstraint;
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;
@end
@implementation WCRegisterViewController
- (IBAction)textChanged:(UITextField *)sender {
    WCLog(@"%@",sender.text);
    BOOL enabled = (self.userField.text.length!=0&&self.pwdField.text.length!=0);
    self.registerBtn.enabled = enabled;
}
- (IBAction)registerBtnClick {
    [self.view endEditing:YES];
    
    //判断用户输入的是否是手机号码
    if ([self.userField isTelphoneNum]) {
        [MBProgressHUD showError:@"请输入正确的手机号码"];
        return;
    }
    //1.把用户输入的数据保存到单例中
    [WCUserInfo sharedWCUserInfo].registerUsr = self.userField.text;
    [WCUserInfo sharedWCUserInfo].registerPwd = self.pwdField.text;
    
//    AppDelegate *app = [UIApplication sharedApplication].delegate;
//    app.registerOperation = YES;
    //注册给提示
    [WCXMPPTool sharedWCXMPPTool].registerOperation = YES;
    [MBProgressHUD showMessage:@"正在注册中" toView:self.view];
    __weak typeof(self) selfVC = self;
    
    [[WCXMPPTool sharedWCXMPPTool] xmppUserRegister:^(XMPPResultType type) {
        [selfVC handleResultType:type];
    }];
    
}
- (IBAction)cancel {
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)handleResultType:(XMPPResultType)type{
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:self.view];
        switch (type) {
            case XMPPResultTypeRegisterSuccess:
                [self dismissViewControllerAnimated:YES completion:nil];
                if ([self.delegate respondsToSelector:@selector(registerViewControllerDidFinishRegister)]) {
                    [self.delegate registerViewControllerDidFinishRegister];
                }
                break;
                
            case XMPPResultTypeRegisterFailure:
                [MBProgressHUD showError:@"注册失败，用户名重复"];
                break;
                
            case XMPPResultTypeNetErr:
                [MBProgressHUD showError:@"网络问题"];
                break;
        } });
    
}


#pragma mark - lifeCircle
-(void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"其他方式登录";
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone) {
        self.leftConstraint.constant = 10;
        self.rightConstraint.constant = 10;
    }
    self.userField.background = [UIImage stretchedImageWithName:@"operationbox_text"];
    self.pwdField.background = [UIImage stretchedImageWithName:@"operationbox_text"];
    [self.registerBtn setResizeN_BG:@"fts_green_btn" H_BG:@"fts_green_btn_HL"];
}

@end
